﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TSTEKVR
    {

        public TSTEKVR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVRKOPF();
            DataContext = MV;
            AUFTYP = "B";
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
            AUFTYP = "B";
        }
        public override void OnAppModeChg()
        {
            base.OnAppModeChg();
            switch (app.company)
            {
                case "TST":
                    KRS = MV.year.Substring(2, 2);
                    switch (app.AppMode)
                    {
                        case "TISSUE":
                            //KRS = MV.year.Substring(3, 1)+ "S";
                            break;
                        case "TMODE":
                            //KRS = MV.year.Substring(3, 1)+"M";
                            break;

                    }
                    break;
            }
        }

        private void AUF_Enter(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KUNDNR = "";
                VVKP = "";
                KEY = "";
                //VRP = "";
            }
            else
            {
                KEY = dwMaster["VRKRENR"].ToString();
                KUNDNR = dwMaster["VRKKUNDNR"].ToString();
                VVKP = dwMaster["VRPAUFKRS"].ToString() + dwMaster["VRPAUFLFD"].ToString();
                //VRP = dwMaster["VRPRENR"].ToString() + dwMaster["VRPREPOS"].ToString(); 
            }
        }

        //待处理的尾款
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            if (LFD == "000000") LFD = "";
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKRENR"] = ART;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '" + LFD + "%'";
            if (!IsPaid)
            {
                dwData["VRKFILLER"] = " AND VRPZOLLTNR LIKE '0:%' AND VRPBONKZ='N'";
            }
            else
            {
                dwData["VRKFILLER"] = " AND VRPZOLLTNR LIKE '0:%' ";
            }
            SResult = TMV.QVRK04(dwData);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }

        //预付款
        private void EKKPaymentCommand(object sender, RoutedEventArgs e)
        {
           
            dwData = TBMain.NewRow();
            if (LFD == "000000") LFD = "";
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '" + LFD + "%'";
            
            string filter = "AND VRPZOLLTNR LIKE '1:%'";
            if (LIEFNR.Length > 3)
            {
                filter += "  AND VRPVLIEFNR='" + LIEFNR + "'";
            } 
            if (!IsPaid)
            {
                filter += "  AND VRPBONKZ='N'";
            }
            dwData["VRKFILLER"] = filter;

            SResult = TMV.QVRK04(dwData);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }
        //中款
        private void VVPaymentCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            if (LFD == "000000") LFD = "";
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '" + LFD + "%'";
          
            string filter = "AND VRPZOLLTNR LIKE '2:%'";
            if (LIEFNR.Length > 3)
            {
                filter += "  AND VRPVLIEFNR='" + LIEFNR + "'";
            }
             
            if (!IsPaid)
            {
                filter += "  AND VRPBONKZ='N'";
            }
            dwData["VRKFILLER"] = filter;
            SResult = ((VVRKOPF)MV).QueryVRKOPF(dwData, 4);
            SResult = TMV.QVRK04(dwData);
            base.QueryCommand(sender, e);
        }
        //尾款
        private void RESTPaymentCommand(object sender, RoutedEventArgs e)
        {
            if (LFD == "000000") LFD = "";
            dwData = TBMain.NewRow();
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '" + LFD + "%'";
            
            string filter = "AND VRPZOLLTNR LIKE '3:%'";
            if (LIEFNR.Length > 3)
            {
                filter += "  AND VRPVLIEFNR='" + LIEFNR + "'";
            }
           
            if (!IsPaid)
            {
                filter += "  AND VRPBONKZ='N'";
            }
            dwData["VRKFILLER"] = filter;
            SResult = TMV.QVRK04(dwData);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }

        private void OpenVRCommand(object sender, RoutedEventArgs e)
        {
            if (LFD == "000000") LFD = "";
            dwData = TBMain.NewRow();
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '" + LFD + "%'";
             
            string filter = "AND SUBSTR(VRPZOLLTNR,1,2)<>'0:'";
            if (LIEFNR.Length > 3)
            {
                filter += "  AND VRPVLIEFNR='" + LIEFNR + "'";
            } 
            if (!IsPaid)
            {
                filter += "  AND VRPBONKZ='N'";
            }
            dwData["VRKFILLER"] = filter;
            SResult = TMV.QVRK04(dwData);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);

        }
        public void CommissionCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一个发票行");
                return;
            }
            PAYPAL = 9;
            PM["RENR"] = KEY;
            PM["PAYPAL"] = PAYPAL;
            PM["ORDERNUM"] = VVKP;
            PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
            PM["ZAHLBED"] = ZAHLBED;
            PM["AUFTYP"] = "B";
            TMV.VRPCM(PM);
        }
    }
}
